package io.github.uicdb.liba.api.multiblock;

import net.minecraft.core.Registry;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.util.INBTSerializable;

public class FormingToolHolder implements INBTSerializable<CompoundTag> {
    private Item formingTool;
    private boolean shouldConsume;

    public FormingToolHolder(Item formingTool, boolean shouldConsume) {
        this.formingTool = formingTool;
        this.shouldConsume = shouldConsume;
    }

    @Override
    public CompoundTag serializeNBT() {
        CompoundTag nbt = new CompoundTag();
        nbt.putString("Item", formingTool.getRegistryName().toString());
        nbt.putBoolean("ShouldConsume", shouldConsume);
        return nbt;
    }

    @Override
    public void deserializeNBT(CompoundTag nbt) {
        formingTool = Registry.ITEM.get(new ResourceLocation(nbt.getString("Item")));
        shouldConsume = nbt.getBoolean("ShouldConsume");
    }

    public boolean usingTool(ItemStack itemStack) {
        if (itemStack.isEmpty() || !itemStack.getItem().equals(formingTool)) return false;
        if (shouldConsume) itemStack.setDamageValue(itemStack.getDamageValue() - 1);
        return true;
    }
}
